#include "../source/message.hpp"
#include <iostream>

int main()
{
    // JsonMuduoRPC::RPCRequest::ptr rrptr = JsonMuduoRPC::MessageFactory::create<JsonMuduoRPC::RPCRequest>();
    // rrptr->setMethod("add");
    // Json::Value val;
    // val["num_1"] = 1;
    // val["num_2"] = 2;
    // rrptr->setParams(val);

    // std::string body;
    // rrptr->serialize(body);
    // std::cout << body << std::endl;

    // JsonMuduoRPC::BaseMessage::ptr bmptr = JsonMuduoRPC::MessageFactory::create(JsonMuduoRPC::MType::REQ_RPC); 
    // bmptr->unSerialize(body);
    // bool ret = bmptr->cheak();
    // if (ret == false)
    //     return -1;
    // JsonMuduoRPC::RPCRequest::ptr rrptr2 = std::dynamic_pointer_cast<JsonMuduoRPC::RPCRequest>(bmptr);
    // std::cout << rrptr2->getMethod() << std::endl;
    // val = rrptr2->getParams();
    // std::cout << val["num_1"].asInt() << val["num_2"].asInt() << std::endl;

    // JsonMuduoRPC::TopicRequest::ptr trptr = JsonMuduoRPC::MessageFactory::create<JsonMuduoRPC::TopicRequest>();
    // trptr->setTopic("music");
    // trptr->setOpType(JsonMuduoRPC::TopicOpType::TOPIC_PUBLISH);
    // trptr->setMessage("hello world!");

    // std::string body;
    // trptr->serialize(body);
    // std::cout << body << std::endl;

    // JsonMuduoRPC::BaseMessage::ptr bmptr = JsonMuduoRPC::MessageFactory::create(JsonMuduoRPC::MType::REQ_TOPIC); 
    // bmptr->unSerialize(body);
    // bool ret = bmptr->cheak();
    // if (ret == false)
    //     return -1;
    // JsonMuduoRPC::TopicRequest::ptr trptr2 = std::dynamic_pointer_cast<JsonMuduoRPC::TopicRequest>(bmptr);
    // std::cout << trptr2->getTopic() << std::endl;
    // std::cout << (int)trptr2->getOpType() << std::endl;
    // std::cout << trptr2->getMessage() << std::endl;

    
    // JsonMuduoRPC::ServiceRequest::ptr srptr = JsonMuduoRPC::MessageFactory::create<JsonMuduoRPC::ServiceRequest>();
    // srptr->setMethod("add");
    // srptr->setOpType(JsonMuduoRPC::ServiceOpType::SERVICE_REGISTRY);
    // JsonMuduoRPC::Address val;
    // val.first = "127.0.0.1";
    // val.second = 8080;
    // srptr->setHost(val);

    // std::string body;
    // srptr->serialize(body);
    // std::cout << body << std::endl;

    // JsonMuduoRPC::BaseMessage::ptr bmptr = JsonMuduoRPC::MessageFactory::create(JsonMuduoRPC::MType::REQ_SERVICE); 
    // bmptr->unSerialize(body);
    // bool ret = bmptr->cheak();
    // if (ret == false)
    //     return -1;
    // JsonMuduoRPC::ServiceRequest::ptr srptr2 = std::dynamic_pointer_cast<JsonMuduoRPC::ServiceRequest>(bmptr);
    // std::cout << srptr2->getMethod() << std::endl;
    // std::cout << (int)srptr2->getOpType() << std::endl;
    // std::cout << srptr2->getHost().first << " " << srptr2->getHost().second << std::endl;

        
    // JsonMuduoRPC::RPCResponse::ptr rrptr = JsonMuduoRPC::MessageFactory::create<JsonMuduoRPC::RPCResponse>();
    // rrptr->setRCode(JsonMuduoRPC::RCode::RCODE_OK);
    // rrptr->setResult(33);

    // std::string body;
    // rrptr->serialize(body);
    // std::cout << body << std::endl;

    // JsonMuduoRPC::BaseMessage::ptr bmptr = JsonMuduoRPC::MessageFactory::create(JsonMuduoRPC::MType::RSP_RPC); 
    // bmptr->unSerialize(body);
    // bool ret = bmptr->cheak();
    // if (ret == false)
    //     return -1;
    // JsonMuduoRPC::RPCResponse::ptr rrptr2 = std::dynamic_pointer_cast<JsonMuduoRPC::RPCResponse>(bmptr);
    // std::cout << (int)rrptr2->getRCode() << std::endl;
    // std::cout << rrptr2->getResult() << std::endl;

    JsonMuduoRPC::ServiceResponse::ptr srptr = JsonMuduoRPC::MessageFactory::create<JsonMuduoRPC::ServiceResponse>();
    srptr->setMethod("add");
    srptr->setOpType(JsonMuduoRPC::ServiceOpType::SERVICE_DISCOVERY);
    srptr->setRCode(JsonMuduoRPC::RCode::RCODE_OK);
    // Json::Value hosts;
    // Json::Value v1, v2;
    // v1[KEY_HOST_IP] = "127.0.0.1", v2[KEY_HOST_IP] = "127.0.0.2";
    // v1[KEY_HOST_PORT] = 8080, v2[KEY_HOST_PORT] = 8081;

    // hosts.append(v1);
    // hosts.append(v2);
    std::vector<JsonMuduoRPC::Address> hosts;

    //写法一：
    JsonMuduoRPC::Address add1("127.0.0.1", 8080);
    JsonMuduoRPC::Address add2("127.0.0.2", 8081);
    hosts.push_back(add1);
    hosts.push_back(add2);

    //写法二：
    hosts.emplace_back(std::make_pair("127.0.0.1", 8080), std::make_pair("127.0.0.2", 8081));
    
    srptr->setHosts(hosts);


    std::string body;
    srptr->serialize(body);
    std::cout << body << std::endl;

    JsonMuduoRPC::BaseMessage::ptr bmptr = JsonMuduoRPC::MessageFactory::create(JsonMuduoRPC::MType::RSP_SERVICE); 
    bmptr->unSerialize(body);
    bool ret = bmptr->cheak();
    if (ret == false)
        return -1;
    JsonMuduoRPC::ServiceResponse::ptr srptr2 = std::dynamic_pointer_cast<JsonMuduoRPC::ServiceResponse>(bmptr);
    std::cout << srptr2->getMethod() << std::endl;
    std::cout << (int)srptr2->getOpType() << std::endl;

    hosts = srptr2->getHosts();
    for (auto & add : hosts)
        std::cout << add.first << " " << add.second << std::endl;
    return 0;
}























// #include <random>
// #include <iomanip>
// #include <sstream>
// #include <atomic>
// #include <iostream>

// const std::string getUuidStr()
// {
//     // UUID: 8-4-4-4-12的32个16进制数

//     std::stringstream ss; // 用于存放uuid的字符流

//     // 1. 创建一个机器随机数对象
//     std::random_device rd;
//     // 2. 将机器随机数作为种子，生成伪随机对象
//     std::mt19937 genenrator(rd());
//     // 3. 限制随机数范围 [0, 255]
//     std::uniform_int_distribution<int> distibution(0, 255);

//     // 随机生成8个16进制随机数 num1num2num3num4-num5num6-num7num8
//     for (int i = 0; i < 8; i++)
//     {
//         if (i == 4 || i == 6)
//             ss << '-';
//         ss << std::setw(2) << std::setfill('0') << std::hex << distibution(genenrator);
//     }

//     ss << '-';

//     static std::atomic<size_t> seq(1); // 序号，每次加一
//     size_t cur = seq.fetch_add(1);

//     // 还剩8个字节，每次取前1个字节（8位）
//     for (int i = 7; i >= 0; --i)
//     {
//         if (i == 5)
//             ss << '-';
//         ss << std::setw(2) << std::setfill('0') << std::hex << ((cur >> (i * 8)) & 0xff);
//     }

//     return ss.str();
// }

// int main()
// {

//     for (int i = 0; i < 10; i++)
//         std::cout << getUuidStr() << std::endl;

//     return 0;
// }